package com.tuosha.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irs.annotation.SysLog;
import com.irs.util.ResultUtil;
import com.sun.xml.internal.xsom.impl.scd.Iterators;
import com.tuosha.base.controller.BaseController;
import com.tuosha.constant.Constants;
import com.tuosha.model.JieQianJqCatEntity;
import com.tuosha.model.JieQiansEntity;
import com.tuosha.model.JqCatsEntity;
import com.tuosha.service.JieQianJqCatService;
import com.tuosha.service.JieQiansService;
import com.tuosha.service.JqCatsService;
import com.tuosha.service.impl.JieQianJqCatServiceImpl;
import com.tuosha.util.Query;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author tuosha
 * @email 1044053532@qq.com
 * @date 2018-07-25 09:38:58
 */
@Controller
@RequestMapping("jieqians")
public class JieQiansController extends BaseController {
	@Autowired
	private JieQiansService jieQiansServiceImpl;

	@Autowired
	private JqCatsService jqCatsServiceImpl;

    @Autowired
    private JieQianJqCatService jieQianJqCatServiceImpl;
	/**
	 * 页面
	 */
	@RequestMapping("/page")
	public String page(@RequestParam Map<String, Object> params){
		return "page/kouzijihe/kouzijihe";
	}
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(@RequestParam Map<String, Object> params){
	    Query query = new Query(params);
		List<JieQiansEntity> jieQiansList = jieQiansServiceImpl.queryList(query);
		int total = jieQiansServiceImpl.queryTotal(query);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",total,jieQiansList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping(value="/info/{id}", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object info(@PathVariable("id") Integer id){
		JieQiansEntity jieQians = jieQiansServiceImpl.queryObject(id);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",0,jieQians);
	}

	/**
	 * 增加页面
	 */
	@RequestMapping("addKouzi")
	public String kouziAdd(HttpServletRequest req){
		List<JqCatsEntity> jqcatsEntity = jqCatsServiceImpl.queryAllList();
		req.setAttribute("jqcats",jqcatsEntity);
		return "page/kouzijihe/addKouzi";
	}

	@RequestMapping("/checkKouziName/{name}")
	@ResponseBody
	public ResultUtil checkKouziName(@PathVariable("name")String name) {
		JieQiansEntity kouzijihes = jieQiansServiceImpl.queryName(name);
		if(kouzijihes!=null){
			return new ResultUtil(500,"名称已存在！");
		}
		return new ResultUtil(0);
	}

	/**
	 * 保存
	 */
	@RequestMapping(value="/save", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object save(@ModelAttribute JieQiansEntity jieQians){
		jieQiansServiceImpl.save(jieQians);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",0,jieQians);
	}

	/**
	 * 保存2
	 */
	@RequestMapping(value="/savelist")
	@ResponseBody
	public Object savelist(@RequestParam Map<String, String> map){

		String aa = JSONUtils.toJSONString(map);

		jieQiansServiceImpl.savelist(JSONObject.parseObject(aa));

		return putMsgToJsonString(Constants.WebSite.SUCCESS,"保存成功",0,map);
	}
	/**
	 * 修改待信息显示
	 */

	@RequestMapping(value="/editKouzi/{id}")
	@RequiresPermissions("jieqians:jieqians:update")
	public String editXYK(HttpServletRequest req, @PathVariable("id")Integer id) {
		JieQiansEntity jieQiansEntity = jieQiansServiceImpl.queryObject(id);
        List<JqCatsEntity> jqcatsEntity = jqCatsServiceImpl.queryAllList();
        List<JieQianJqCatEntity> jq = jieQianJqCatServiceImpl.queryAllList(id);

		req.setAttribute("jieqian",jieQiansEntity);
        req.setAttribute("jqcats",jqcatsEntity);
        req.setAttribute("jq",jq);
		return "page/kouzijihe/editKouzi";
	}
	/**
	 * 修改
	 */
	@RequestMapping(value="/update", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@ModelAttribute JieQiansEntity jieQians){
		int result = jieQiansServiceImpl.update(jieQians);
		return putMsgToJsonString(result,"",0,"");
	}
    /**
     * 修改2
     */
    @RequestMapping(value="/updatelist", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public Object updatelist(@RequestParam Map<String, String> map){
        String aa = JSONUtils.toJSONString(map);
        int result = jieQiansServiceImpl.updatelist(JSONObject.parseObject(aa));
        return putMsgToJsonString(result,"修改成功",0,"map");
    }
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(@RequestParam Integer[] ids){
		int result = jieQiansServiceImpl.deleteBatch(ids);
		return putMsgToJsonString(result,"",0,"");
	}

	@SysLog(value="删除口子信息")
	@RequestMapping("/delById/{id}")
	@RequiresPermissions("jieqians:jieqians:delete")
	@ResponseBody
	public ResultUtil delById(@PathVariable("id")Integer id) {

		try {
			jieQiansServiceImpl.delById(id);
			return ResultUtil.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error();
		}
	}

	/**
	 * 批量删除指定管理员
	 * @param //id
	 * @return
	 */
	@SysLog(value="批量删除口子信息")
	@RequestMapping("/delPatch/{Str}")
	@RequiresPermissions("jieqians:jieqians:delete")
	@ResponseBody
	public ResultUtil delPatch(@PathVariable("Str")String xykStr) {

		try {
			jieQiansServiceImpl.delPatch(xykStr);
			return ResultUtil.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error();
		}

	}
	
}
